<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="算法名称" prop="methodName">
          <el-input
            v-model="queryParams.methodName"
            placeholder="请输入算法名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <!-- <el-form-item label="访问参数样例" prop="methodParam">
          <el-input
            v-model="queryParams.methodParam"
            placeholder="请输入访问参数样例"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
        <!-- <el-form-item label="方法调用次数" prop="methodNum">
          <el-input
            v-model="queryParams.methodNum"
            placeholder="请输入方法调用次数"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
        <!-- <el-form-item label="模块" prop="methodCreateby">
          <el-input
            v-model="queryParams.methodCreateby"
            placeholder="请输入模块"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
        <!-- <el-form-item label="创建时间" prop="createtime">
          <el-input
            v-model="queryParams.createtime"
            placeholder="请输入创建时间"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
        <!-- <el-form-item label="方法更新时间" prop="updatetime">
          <el-date-picker clearable
            v-model="queryParams.updatetime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择方法更新时间">
          </el-date-picker>
        </el-form-item> -->
        <!-- <el-form-item label="方法调用地址" prop="methodUrl">
          <el-input
            v-model="queryParams.methodUrl"
            placeholder="请输入方法调用地址"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
    
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:method:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:method:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:method:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:method:export']"
          >导出</el-button>
        </el-col>
        <el-col :span="1.5">
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-col>
        <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="methodList" @selection-change="handleSelectionChange">
        <el-table-column show-overflow-tooltip type="selection" width="55" align="center" />
        <!-- <el-table-column show-overflow-tooltip label="方法id" align="center" prop="id" /> -->
        <el-table-column show-overflow-tooltip label="方法名称" align="center" prop="methodName" />
        <el-table-column show-overflow-tooltip label="访问方式" align="center" prop="methodType" />
        <el-table-column show-overflow-tooltip label="访问参数样例" align="center" prop="methodParam" />
        <!-- <el-table-column show-overflow-tooltip label="方法调用次数" align="center" prop="methodNum" /> -->
        <el-table-column show-overflow-tooltip label="模块" align="center" prop="methodCreateby" />
        <!-- <el-table-column show-overflow-tooltip label="方法创建时间" align="center" prop="createtime" /> -->
        <!-- <el-table-column show-overflow-tooltip label="方法更新时间" align="center" prop="updatetime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updatetime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column> -->
        <el-table-column show-overflow-tooltip label="方法调用地址" align="center" prop="methodUrl" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleGoing(scope.row)"
            >运行</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleJobLog(scope.row)"
            >调用日志</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:method:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:method:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改课题算法管理对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="方法名称" prop="methodName">
                <el-input v-model="form.methodName" placeholder="请输入方法名称" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="访问方式" prop="methodType">
                <el-input v-model="form.methodType" placeholder="请输入访问方式" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="方法调用次数" prop="methodNum">
                <el-input v-model="form.methodNum" placeholder="请输入方法调用次数" />
              </el-form-item>
            </el-col>
          </el-row>
          
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="模块" prop="methodCreateby">
                <el-input v-model="form.methodCreateby" placeholder="请输入模块" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="方法调用地址" prop="methodUrl">
                <el-input v-model="form.methodUrl" placeholder="请输入方法调用地址" />
              </el-form-item>
            </el-col>


          </el-row>
          <el-row :gutter="20">
            <el-form-item label="访问参数样例" prop="methodParam">
                <!-- 改成大的textarea 自动换行   -->
                <el-input type="textarea" v-model="form.methodParam" placeholder="请输入访问参数样例" :autosize="true" />
              </el-form-item>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
       <!-- 算法详细 -->
    <el-dialog title="任务详细" :visible.sync="openView" width="700px" append-to-body>
      <el-form ref="form" :model="form" label-width="120px" size="mini">
        <el-row>
          <el-col :span="12">
            <el-form-item label="任务编号：">{{ form.jobId }}</el-form-item>
            <el-form-item label="任务名称：">{{ form.jobName }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间：">{{ form.createTime }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="cron表达式：">{{ form.cronExpression }}</el-form-item>
          </el-col>
          <el-col :span="12">
          </el-col>
          <el-col :span="24">
            <el-form-item label="调用目标方法：">{{ form.invokeTarget }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务状态：">
              <div v-if="form.status == 0">正常</div>
              <div v-else-if="form.status == 1">暂停</div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否并发：">
              <div v-if="form.concurrent == 0">允许</div>
              <div v-else-if="form.concurrent == 1">禁止</div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="执行策略：">
              <div v-if="form.misfirePolicy == 0">默认策略</div>
              <div v-else-if="form.misfirePolicy == 1">立即执行</div>
              <div v-else-if="form.misfirePolicy == 2">执行一次</div>
              <div v-else-if="form.misfirePolicy == 3">放弃执行</div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openView = false">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 算法运行结果 -->
    <el-dialog :title="'运行结果'" :visible.sync="goingResult" width="70%" append-to-body>
      <el-descriptions v-if="goingResult" :column="1" border>
        <el-descriptions-item label="状态码">
          {{ goingResult.code }}
        </el-descriptions-item>
        <el-descriptions-item label="结果">
          {{ goingResult.data }}
        </el-descriptions-item>
        <el-descriptions-item label="信息">
          {{ goingResult.msg }}
        </el-descriptions-item>
      </el-descriptions>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>

  import { listMethod, getMethod, delMethod, addMethod, updateMethod,going } from "@/api/system/algorithm";
  
  export default {
    name: "Method",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 课题算法管理表格数据
        methodList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否显示详细弹出层
        openView: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          methodName: null,
          methodType: null,
          methodParam: null,
          methodNum: null,
          methodCreateby: null,
          createtime: null,
          updatetime: null,
          methodUrl: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        },
        // 算法运行结果
        goingResult:null,
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询课题算法管理列表 */
      getList() {
        this.loading = true;
        listMethod(this.queryParams).then(response => {
          this.methodList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.goingResult=null;
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          methodName: null,
          methodType: null,
          methodParam: null,
          methodNum: null,
          methodCreateby: null,
          createtime: null,
          updatetime: null,
          methodUrl: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
         /** 任务详细信息 */
    handleView(row) {
        console.log(row);
        getMethod(row.id).then(response => {
        console.log(response.data);

        this.form = response.data;
        this.openView = true;
      });
    },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加课题算法";
      },
      // 运行算法
      handleGoing(row){
        console.log(row);
        let obj={methodName:row.methodName,methodName:row.methodName,methodType:row.methodType,methodParam:row.methodParam,methodNum:row.methodNum,methodCreateby:row.methodCreateby,methodUrl:row.methodUrl}
       going(obj).then(res=>{
          this.goingResult=res
        })
      },

         /** 任务日志列表查询 */
    handleJobLog(row) {
      const id = row.id || 0;
      this.$router.push('/system/algorithm-log/index/' + id)
    },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getMethod(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改课题算法";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateMethod(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addMethod(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除课题算法管理编号为"' + ids + '"的数据项？').then(function() {
          return delMethod(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/method/export', {
          ...this.queryParams
        }, `method_${new Date().getTime()}.xlsx`)
      }
    }
  };
  </script>
  